package cn.com.yx.ms.controller;

import cn.com.yx.ms.application.web.ApiQuery;
import cn.com.yx.ms.application.web.ApiQueryBase;
import cn.com.yx.ms.domain.TestModel;
import cn.com.yx.ms.service.*;
import cn.yx.framework.configuration.ApiCode;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.configuration.BaseConstant;
import cn.yx.framework.util.DateUtil;
import cn.yx.framework.util.MsgPropertyUtil;
import cn.yx.framework.view.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

// import org.java_websocket.drafts.Draft_17;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "hardware")
@Api(value = "设备板卡管理", description = "设备板卡管理")
public class HardwareController {

    private static final Logger logger = LoggerFactory.getLogger(HardwareController.class);

    @Resource
    private MessageSource messageSource;
    @Resource
    private ISysService iSysService;
    @Resource
    private ICommonService iCommonService;

    @Resource
    private ITestService iTestService;
    @Resource
    private IDevService iDevService;
    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private IHardwareService iHardwareService;

    //    @Resource
    private Jedis jedis;

    @PostMapping()
    @ApiOperation(value = "创建测试实体", notes = "创建测试实体")
    @ApiImplicitParam(paramType = "body", name = "testmode", dataType = "Map", required = true, value = "测试实体详细信息")
    public ApiResult create(@RequestBody @Validated TestModel testModel, BindingResult bindingResult) {
        String uuid = UUID.randomUUID().toString();
        String capText = DateUtil.date2String(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        testModel.setId(uuid);
        redisTemplate.opsForValue().set(uuid, testModel, 600 * 5, TimeUnit.SECONDS);

        return new ApiResult<>(AppConstant.MSG_SUCCESS, true, "", iTestService.insert(testModel));
    }


    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    @ApiOperation("获取带分页的寻人寻亲数据")
    public ApiResult find(@ApiQuery ApiQueryBase query, HttpServletRequest request) {

        System.out.println(BaseConstant.getAesConfig().getSecretKey());

        query.getConditions().put("ps", request.getParameter("ps"));
        query.getConditions().put("pi", request.getParameter("pi"));
        query.getConditions().put("title", request.getParameter("title"));

        return new ApiResult<>(AppConstant.MSG_SUCCESS, iTestService.find(query));
    }

    @ResponseBody
    @RequestMapping(value = "import", method = RequestMethod.POST)
    @ApiOperation("导入板卡信息")
    public ApiResult importModel(HttpServletRequest request){
        return iHardwareService.importHardWare(request);
    }

    @ResponseBody
    @RequestMapping(value = "initConfig/{hardwareuniqueno}", method = RequestMethod.GET)
    @ApiOperation("获取板卡初始化配置")
    public ApiResult getInitConfig(@PathVariable("hardwareuniqueno") String hardwareuniqueno) throws Exception {
        ApiCode apiCode = iHardwareService.pushDevConfig(hardwareuniqueno);
        return new ApiResult<>(Integer.parseInt(apiCode.msgId),
                apiCode.equals(ApiCode.APP_100200) || apiCode.equals(ApiCode.APP_101400),
                MsgPropertyUtil.getProperty(apiCode.msgId),hardwareuniqueno);

    }

    @ResponseBody
    @RequestMapping(value = "datas", method = RequestMethod.GET)
    @ApiOperation("获取所有板卡清单")
    public ApiResult findList(HttpServletRequest request) {
        return new ApiResult<>(AppConstant.MSG_SUCCESS, iHardwareService.findDatas());
    }

    /**
     * 扫码
     *
    */
    @RequestMapping("one/{uniqu1no}/equipmentno")
    @ApiOperation(value = "设备式样同步", notes = "设备式样同步")
    @ApiImplicitParam(paramType = "String", name = "uniqu1no", dataType = "String", required = true, value = "设备实体详细信息")
    public ApiResult getEquipmentNoAndStyleId(@PathVariable("uniqu1no") String uniqu1no) throws Exception {
        return new ApiResult(AppConstant.MSG_SUCCESS,
                true,
                null,
                iDevService.findEquipmentNoAndStyleId(uniqu1no));
    }

    @ResponseBody
    @RequestMapping(value= "catalogs",method = RequestMethod.GET)
    @ApiOperation("获取站台目录树")
    public ApiResult getHardWares(){
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                "",
                iHardwareService.getHardWares());
    }

    @ResponseBody
    @RequestMapping(value="sync/{hardwareuniqueno}",method = RequestMethod.GET)
    @ApiOperation("同步操作")
    public ApiResult hardwareSync(@PathVariable("hardwareuniqueno") String hardwareuniqueno){
        return new ApiResult<>(AppConstant.MSG_SUCCESS,
                true,
                "",
                iHardwareService.hardwareSync(hardwareuniqueno));
    }


}